<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-table
        :data="tableData"
        border
        @row-dblclick="getUserDetails"
        style="width: 100%">
      <el-table-column prop="id" label="ID" align="center"></el-table-column>
      <el-table-column prop="username" label="名称" width="110px" align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="100px" align="center"></el-table-column>
      <el-table-column prop="avatar" label="相册" align="center">
        <template slot-scope="scope">
          <div class="demo-image">
            <div class="block">
              <span class="demonstration"></span>
              <el-image
                  style="width: 70px; height: 70px"
                  :src="scope.row.avatar"></el-image>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号码" width="110px" align="center"></el-table-column>
      <el-table-column prop="enable" label="是否启用" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="changeEnable(scope.row)"
              v-model="scope.row.enable"
              active-color="#13ce66"
              inactive-color=""
              :active-value="1"
              :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click.stop="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>


<script>
export default {
  data() {
    return {
      tableData: [],
    }
  },
  methods: {
    changeEnable(user) {
      let enableText = ['禁用', '启用'];
      let url = 'http://localhost:9080/users/' + user.id;
      if (user.enable == 1) {
        console.log("将启用管理员");
        url += '/enable'
      } else {
        console.log("将禁用管理员")
        url += '/disable'
      }
      console.log('url=' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        if (response.data.state == 20000) {
          let message = '将用户【' + user.username + '】的启用状态改为【' + enableText[user.enable] + '】成功！';
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(response.data.message);
        }
        if (response.data.state == 40400) {
          this.loadUserList();
        }
      });
    },

    loadUserList() {
      console.log('在localStorage中的JWT数据：' + localStorage.getItem('jwt'));
      let url = 'http://localhost:9080/users';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        console.log(response.data.data);
        this.tableData = response.data.data;
      })
    },

    openDeleteConfirm(user) {
      this.$confirm('此操作将永久删除【' + user.username + '】用户，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(user);
      }).catch(() => {
      });
    },

    handleDelete(user) {
      let url = 'http://localhost:9080/users/' + user.id + '/delete';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        if (response.data.state != 20000) {
          this.$message.error(response.data.message);
        }
        if (response.data.state == 20000 || response.data.state == 40400) {
          this.loadUserList();
        }
      })
    },

    getUserDetails(row){
      console.log(row.id);
      localStorage.setItem('userId', row.id);
      this.$router.push('/sys-admin/user/details');
    }

  },
  mounted() {
    this.loadUserList();
  }
}
</script>

<style scoped>

</style>
